#include "instruct.h"
#include "register.h"
#include "def.h"
#include <stdio.h>

int divIsRun(char opt){
    if(opt==DIVRN){
        return 6;
    }
    if(opt==DIVRR){
        return 3;
    }
    return 0;
}
int divParseRun(Register*R){
    char*code=R->code;
    int pc=R->RPC;
    char opt=code[pc];
    if(opt==DIVRR){
        char r1=code[pc+1];
        char r2=code[pc+2];
        u32 num2=0;
        

        if(r2==AX){
            num2=R->RAX;
        }else if(r2==BX){
            num2=R->RBX;
        }else if(r2==CX){
            num2=R->RCX;
        }else if(r2==DX){
            num2=R->RDX;
        }else if(r2==EX){
            num2=R->REX;
        }else if(r2==BP){
            num2=R->RBP;
        }else if(r2==SS){
            num2=R->RSS;
        }else if(r2==SP){
            num2=R->RSP;
        }

        if(r1==AX){
            R->RAX=R->RAX/num2;
        }else if(r1==BX){
            R->RBX=R->RBX/num2;
        }else if(r1==CX){
            R->RCX=R->RCX/num2;
        }else if(r1==DX){
            R->RDX=R->RDX/num2;
        }else if(r1==EX){
            R->REX=R->REX/num2;
        }else if(r1==BP){
            R->RBP=R->RBP/num2;
        }else if(r1==SS){
            R->RSS=R->RSS/num2;
        }else if(r1==SP){
            R->RSP=R->RSP/num2;
        }
    }else if(opt==DIVRN){
        char r1=code[pc+1];
        u32 num2=byte2int(code[pc+5],code[pc+4],code[pc+3],code[pc+2]);
       if(r1==AX){
            R->RAX=R->RAX/num2;
        }else if(r1==BX){
            R->RBX=R->RBX/num2;
        }else if(r1==CX){
            R->RCX=R->RCX/num2;
        }else if(r1==DX){
            R->RDX=R->RDX/num2;
        }else if(r1==EX){
            R->REX=R->REX/num2;
        }else if(r1==BP){
            R->RBP=R->RBP/num2;
        }else if(r1==SS){
            R->RSS=R->RSS/num2;
        }else if(r1==SP){
            R->RSP=R->RSP/num2;
        }
    }
    R->RPC+=divIsRun(opt);
    return 0;
}
